import { Store } from 'iflux2';
import webApi from './webApi';
import { fromJS } from 'immutable';

import ListActor from './actor/list-actor';
import CommentActor from './actor/comment-actor';
import PaginationActor from 'actor/pagination';

import { OrderListType, CommentType } from './constant/action-type';
import { PaginationType } from 'actor/pagination/type';


export default class AppStore extends Store {
    bindActor() {
        return [
            new ListActor,
            new CommentActor,
            new PaginationActor
        ]
    }


    /**
     * 查询订单列表
     * @param params
     * @returns {Promise.<void>}
     */
    queryOrderList = async () => {
        const allState = this.state();
        const condition = allState.get('condition')
        const params =  {
            pageNum: allState.getIn(['pagination', 'pageNum']) - 1,
            pageSize: allState.getIn(['pagination', 'pageSize']),
            [condition.get('simpleType')]: condition.get('simpleValue')
        };

        this.dispatch(OrderListType.SET_LOADING, true);
        try {
            const {
                orderPageList: { dataList, ...rest },
                orderMapStatus,
                commentMap,
                commentSunMap,
                commentReplyMap } = await webApi.queryOrderList(params);
            // 设置加载状态    
            this.dispatch(OrderListType.SET_LOADING, false);
            // 设置订单数据
            this.dispatch(OrderListType.SET_LIST, { list: fromJS(dataList || []), orderMapStatus: fromJS(orderMapStatus) });
            // 设置评价数据
            this.dispatch(CommentType.INIT_COMMENT_LIST, fromJS(commentMap))
            this.dispatch(CommentType.INIT_SUN_LIST, fromJS(commentSunMap))
            this.dispatch(CommentType.INIT_REPLY_LIST, fromJS(commentReplyMap))
            // 设置分页信息
            rest.pageNum += 1;
            this.dispatch(PaginationType.SET_PAGINATION_PARAM, fromJS(rest))
        } catch (err) {
            this.dispatch(OrderListType.SET_LOADING, false);
            return Promise.reject(err);
        }
    }


    /**
     * 设置分页信息
     * @param pagination
     */
    setPagination = (pagination) => {
        this.dispatch(PaginationType.CHANGE_PAGE_PARAM, fromJS(pagination));
    }

    /**
     * 重置分页信息
     */
    resetPagination = () => {
        this.dispatch(PaginationType.RESET_PAGINATION_PARAM);
    }

    /**
     * 设置查询条件
     */
    setCondition = (simpleType, simpleValue) => {
        this.dispatch(OrderListType.SET_CONDITION, fromJS({ simpleType, simpleValue }))
    }

    /**
     * 设置评价详情
     */
    setCommentDetail = (oid) => {
        this.dispatch(CommentType.SET_COMMENT_DETAIL, oid)
    }

    /**
     * 添加回复
     */
    addCommentReply = (params = {}) => {
        return webApi.addCommentReply(params).then(reply => {
            this.dispatch(CommentType.ADD_COMMENT_REPLY, fromJS(reply));
        }).catch(err => {
            return Promise.reject(err);
        })
    }

    updateCommentShowState = async (params = {}) => {
        try {
            await webApi.updateCommentShowState({
                'id': params.commentId,
                'showState': params.showState
            })
            this.dispatch(CommentType.SET_COMMENT_STATE, params)
        } catch (err) {
            return Promise.reject(err);
        }
    }

    updateReplyShowState = async (params = {}) => {
        try {
            await webApi.updateReplyShowState(params)
            this.dispatch(CommentType.SET_REPLY_STATE, params)
        } catch (err) {
            return Promise.reject(err);
        }
    }

    deleteComments = async (params = {}) => {
        try {
            await webApi.deleteComments(params)
            this.dispatch(CommentType.DELETE_COMMENTS, params)
        } catch (err) {
            return Promise.reject(err);
        }
    }


}